//
//  TextMsgCell.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/21.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "TextMsgCell.h"
#import "PPStickerDataManager.h"
#import "NSAttributedString+PPAddition.h"

@interface TextMsgCell ()<YYTextViewDelegate>

@property (nonatomic, strong) MASConstraint *msgContentViewRightLayout;
@property (nonatomic, strong) MASConstraint *msgContentViewLeftLayout;

@property (nonatomic, strong) MASConstraint *msgTextViewWidthLayout;
@property (nonatomic, strong) MASConstraint *msgTextViewHeightLayout;

@end

@implementation TextMsgCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
//    self.msgTextLabel = [UILabel k_labelWithFontSize:13 textColor:UIColor.k_t2Color];
    self.msgTextLabel = [YYTextView new];
    self.msgTextLabel.backgroundColor = UIColor.clearColor;
    self.msgTextLabel.editable = NO;
    self.msgTextLabel.delegate = self;
    self.msgTextLabel.dataDetectorTypes = UIDataDetectorTypeLink;
//    self.msgTextLabel.cancelsTouchesInView = NO;
    self.msgContentViewTapGR.cancelsTouchesInView = NO;
    self.msgTextLabel.selectable = NO;
    self.msgTextLabel.selectable = YES;
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//    paragraphStyle.lineSpacing = 3;
//    paragraphStyle.
    self.msgTextLabel.linkTextAttributes = @{NSForegroundColorAttributeName : UIColor.k_hexColor(0x4BDFF3),
                                             NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle]
                                             };
//    self.msgTextLabel.numberOfLines = 0;
//    self.msgTextLabel.preferredMaxLayoutWidth = Chat_MsgContent_MaxWidth - Chat_MsgContent_Padding*2;
    [self.msgContentView addSubview:self.msgTextLabel];
    self.msgTextLabel.contentInset = UIEdgeInsetsZero;
    self.msgTextLabel.textContainerInset = UIEdgeInsetsZero;
    
    [self.msgTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.mas_offset(-Chat_MsgContent_Padding);
        make.left.top.mas_offset(Chat_MsgContent_Padding);
        self.msgTextViewWidthLayout = make.width.mas_equalTo(50);
        self.msgTextViewHeightLayout = make.height.mas_equalTo(30);
    }];
}

- (void)setMsgModel:(MsgModel *)msgModel {
    [super setMsgModel:msgModel];
    [self setMsg:msgModel.msg];
    if (msgModel.direction == 1) {
        self.msgContentView.backgroundColor = UIColor.k_t1Color;
    } else {
        self.msgContentView.backgroundColor = UIColor.k_hexColor(0xf7f7f7);
    }
    
}
- (void)setMsg:(MsgItem *)msg {

    NSDictionary *attDic = nil;
    if (self.msgModel.direction == MsgDirection_send) {
        attDic = @{ NSFontAttributeName: kUsermanager.msgFont, NSForegroundColorAttributeName: UIColor.whiteColor };
    } else {
        attDic = @{ NSFontAttributeName: kUsermanager.msgFont, NSForegroundColorAttributeName: UIColor.k_t2Color };
    }
    
    NSMutableAttributedString *attributedComment = [[NSMutableAttributedString alloc] initWithString:msg.content?:@"" attributes:attDic];
    
    // 匹配表情
//    [PPStickerDataManager.sharedInstance replaceEmojiForAttributedString:attributedComment font:kUsermanager.msgFont];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 3;
    [attributedComment addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:attributedComment.pp_rangeOfAll];
    [[PPStickerDataManager sharedInstance] replaceEmojiForAttributedString:attributedComment font:kUsermanager.msgFont];
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:CGSizeMake(Chat_MsgContent_MaxWidth - Chat_MsgContent_Padding*2, CGFLOAT_MAX) text:attributedComment];
    self.msgTextViewWidthLayout.offset(layout.textBoundingSize.width);
    self.msgTextViewHeightLayout.offset(layout.textBoundingSize.height);
//    self.msgTextViewWidthLayout.offset(100);
//    self.msgTextViewHeightLayout.offset(50);
    self.msgTextLabel.attributedText = attributedComment;
}

#pragma mark textview delegate
- (BOOL)textView:(YYTextView *)textView shouldTapHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange {
    NSLog(@"\n%s [Line %d]", __PRETTY_FUNCTION__, __LINE__);
    return YES;
}

- (void)textView:(YYTextView *)textView didTapHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange rect:(CGRect)rect {
    NSLog(@"\n%s [Line %d]", __PRETTY_FUNCTION__, __LINE__);
    NSAttributedString *subStr = [textView.attributedText attributedSubstringFromRange:characterRange];
    if (self.didTapLinkBlock) {
        self.didTapLinkBlock(subStr.string);
    }
}


@end
